<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use function Psy\debug;

class UserLevelOverdue extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'user.level_overdue';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '会员到期处理';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        try{
            $default_level_id = 1;
            $now = time();
            DB::table('user_account')
                ->where([
                    ['user_level_id','!=',1],
                    ['time_level','<=',$now],
                ])
                ->orWhere([
                    ['user_level_id','!=',1],
                    ['time_level','=',null],
                ])
                ->update([
                    'user_level_id' => $default_level_id,
                    'time_level'    => null
                ]);
            if(App::environment('local'))
                Log::channel('level_overdue')->info('执行成功');
        }catch (\Exception $exception){
            Log::channel('level_overdue')->info('执行失败',[
                'exception' => $exception->getMessage()
            ]);
        }
    }
}
